﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using Lidgren.Network;
using System.Threading;

namespace NetComps_Server
{
    public enum Headers
    {
        PeopleData,
        PersonName,
        Text
    }

    class Program
    {
        static List<Person> People;
        static NetPeerConfiguration Config;
        static NetServer Server;

        static void Main(string[] args)
        {
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            People = new List<Person>();
            Config = new NetPeerConfiguration("Game");
            Config.Port = 7777;
            Config.MaximumConnections = 200;
            Server = new NetServer(Config);
            Server.Start();
            Console.WriteLine("Server started.");
            Server.RegisterReceivedCallback(new SendOrPostCallback(ReceiveData));

            Console.ReadKey();
        }

        private static void ReceiveData(object peer)
        {    
            NetIncomingMessage incmsg;
            NetOutgoingMessage outmsg;

            while ((incmsg = Server.ReadMessage()) != null)
            {
                switch (incmsg.MessageType)
                {
                    case NetIncomingMessageType.ConnectionApproval:

                        incmsg.SenderConnection.Approve();
                        break;

                    case NetIncomingMessageType.Data:

                        Headers header = (Headers)incmsg.ReadByte();

                        switch(header)
                        {
                            case Headers.PeopleData:
                        
                                Console.WriteLine("Test");
                                outmsg = Server.CreateMessage();
                                outmsg.Write((byte)Headers.PeopleData);
                                People.Add(new Person() { Age = 15, Name = "Yo", IP = incmsg.SenderEndPoint.Address.ToString() });
                                People[People.Count-1].Connection = incmsg.SenderConnection;
                                outmsg.Write(People.Count);
                                foreach (Person person in People) outmsg.WriteAllProperties(person);
                                Server.SendToAll(outmsg, NetDeliveryMethod.UnreliableSequenced);
                                break;

                            case Headers.PersonName:

                                Console.WriteLine("NewName");
                                string name = incmsg.ReadString();

                                foreach (Person person in People)
                                {
                                    if (person.Connection == incmsg.SenderConnection)
                                    {
                                        person.Name = name;
                                    }
                                }
                                
                                outmsg = Server.CreateMessage();
                                outmsg.Write((byte)Headers.PeopleData);
                                outmsg.Write(People.Count);
                                foreach (Person person in People) outmsg.WriteAllProperties(person);
                                Server.SendToAll(outmsg, NetDeliveryMethod.UnreliableSequenced);
                                break;
                        }

                        break;
                }
            }
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string IP { get; set; }
        public NetConnection Connection;
        public Person() { }
    }
}
